home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / rexx / RXPointer.lha / rxpointer / rxpointer.c < prev    next >
C/C++ Source or Header  |  1994-11-01  |  5KB  |  205 lines

  1. /*    Original mouse.c v1.0 by Chris Ludwig
  2. $VER rxpointer v1.1 Geoff Fellows 1-Nov-1994
  3. */
  4.  
  5. #include "rxpointer.h"
  6. #include "simplerexx.h"
  7.  
  8. /* input.device stuff */
  9. struct RXPointerContext {
  10.     AREXXCONTEXT RexxStuff;
  11.     BYTE inputopenerror;
  12.     struct InputEvent phony;
  13.     struct MsgPort *inputdevport;
  14.     struct IOStdReq *inputrequest;
  15. };
  16.  
  17. void Quit(struct RXPointerContext *rxpc, char whytext[],UBYTE level);
  18. void ClickButton(struct RXPointerContext *rxpc, UWORD code);
  19. void MovePointer(struct RXPointerContext *rxpc, LONG mousex, LONG mousey);
  20.  
  21. void Quit(struct RXPointerContext *rxpc, char whytext[],UBYTE level) {
  22.     if (!rxpc->inputopenerror)
  23.         CloseDevice((struct IORequest *) rxpc->inputrequest);
  24.  
  25.     if (rxpc->inputrequest) DeleteStdIO(rxpc->inputrequest);
  26.  
  27.     if (rxpc->inputdevport) DeletePort(rxpc->inputdevport);
  28.  
  29.     FreeARexx(rxpc->RexxStuff);
  30.  
  31.     printf("%s\n",whytext);
  32.     exit(level);
  33. }
  34.  
  35. void ClickButton(struct RXPointerContext *rxpc, UWORD code) {
  36.  
  37.     rxpc->phony.ie_NextEvent = NULL;
  38.     rxpc->phony.ie_Class = IECLASS_RAWMOUSE;
  39.     rxpc->phony.ie_TimeStamp.tv_secs = 0;
  40.     rxpc->phony.ie_TimeStamp.tv_micro = 0;
  41.     rxpc->phony.ie_Code = code;                    /* button down */
  42.     rxpc->phony.ie_Qualifier = 0;
  43.     rxpc->phony.ie_X = 0;
  44.     rxpc->phony.ie_Y = 0;
  45.  
  46.     rxpc->inputrequest -> io_Command = IND_WRITEEVENT;
  47.     rxpc->inputrequest -> io_Flags = 0;
  48.     rxpc->inputrequest -> io_Length = sizeof(struct InputEvent);
  49.     rxpc->inputrequest -> io_Data = (APTR)&rxpc->phony;
  50.  
  51.     DoIO(rxpc->inputrequest);
  52.  
  53.     rxpc->phony.ie_NextEvent = NULL;
  54.     rxpc->phony.ie_Class = IECLASS_RAWMOUSE;
  55.     rxpc->phony.ie_TimeStamp.tv_secs = 0;
  56.     rxpc->phony.ie_TimeStamp.tv_micro = 0;
  57.     rxpc->phony.ie_Code = code | IECODE_UP_PREFIX;    /* button up */
  58.     rxpc->phony.ie_Qualifier = 0;
  59.     rxpc->phony.ie_X = 0;
  60.     rxpc->phony.ie_Y = 0;
  61.  
  62.     rxpc->inputrequest -> io_Command = IND_WRITEEVENT;
  63.     rxpc->inputrequest -> io_Flags = 0;
  64.     rxpc->inputrequest -> io_Length = sizeof(struct InputEvent);
  65.     rxpc->inputrequest -> io_Data = (APTR)&rxpc->phony;
  66.  
  67.     DoIO(rxpc->inputrequest);
  68. }
  69.  
  70.  
  71. void MovePointer(struct RXPointerContext *rxpc, LONG mousex, LONG mousey) {
  72.     /* send phony pointer message to input stream */
  73.  
  74.     rxpc->phony.ie_NextEvent = NULL;
  75.     rxpc->phony.ie_Class = IECLASS_POINTERPOS;
  76.     rxpc->phony.ie_TimeStamp.tv_secs = 0;
  77.     rxpc->phony.ie_TimeStamp.tv_micro = 0;
  78.     rxpc->phony.ie_Code = 0;
  79.     rxpc->phony.ie_Qualifier = 0;
  80.     rxpc->phony.ie_X = mousex;
  81.     rxpc->phony.ie_Y = mousey;
  82.  
  83.     rxpc->inputrequest -> io_Command = IND_WRITEEVENT;
  84.     rxpc->inputrequest -> io_Flags = 0;
  85.     rxpc->inputrequest -> io_Length = sizeof(struct InputEvent);
  86.     rxpc->inputrequest -> io_Data = (APTR)&rxpc->phony;
  87.  
  88.     DoIO(rxpc->inputrequest);
  89. }
  90.  
  91.  
  92. /*
  93.  * A *VERY* simple and simple-minded example of using the SimpleRexx.c code.
  94.  *
  95.  * Note: You will want to RUN this program or have another shell
  96.  *  available such that you can still have access to ARexx...
  97.  */
  98. void main(int argc, char *argv[]) {
  99.     struct Library *RexxSysBase;
  100.     short    loopflag=TRUE;
  101.     struct RXPointerContext rxpc;
  102.  
  103.     int x, y, length;
  104.  
  105.     RexxSysBase = OpenLibrary(RXSNAME, 0);
  106.  
  107.     if ((rxpc.inputdevport=CreatePort(0,0)) == NULL)
  108.         Quit(&rxpc, "Couldn't create a port for input.device",25);
  109.  
  110.     if ((rxpc.inputrequest=CreateStdIO(rxpc.inputdevport)) == NULL)
  111.         Quit(&rxpc, "Couldn't create request block for input device",25);
  112.  
  113.     if ((rxpc.inputopenerror
  114.         =OpenDevice("input.device",0,rxpc.inputrequest,0)) != 0)
  115.         Quit(&rxpc, "Couldn't open input.device",25);
  116.  
  117.     /*
  118.      * Note that SimpleRexx is set up such that you do not
  119.      * need to check for an error to initialize your REXX port
  120.      * This is so your application could run without REXX...
  121.      */
  122.     rxpc.RexxStuff=InitARexx("pointer","pointer");
  123.  
  124.     if (argc) {
  125.         if (rxpc.RexxStuff)
  126.             printf("Send commands to port %s\n",ARexxName(rxpc.RexxStuff));
  127.         else
  128.             printf("ARexx is not available\n");
  129.     }
  130.  
  131.     while (loopflag) {
  132.         ULONG signals=ARexxSignal(rxpc.RexxStuff);
  133.  
  134.         if (signals) {
  135.             struct    RexxMsg        *rmsg;
  136.  
  137.             signals=Wait(signals);
  138.  
  139.             /*
  140.              * Process the ARexx messages...
  141.              */
  142.             while (rmsg=GetARexxMsg(rxpc.RexxStuff)) {
  143.             char    cBuf[24];
  144.             char    *nextchar;
  145.             char    *error=NULL;
  146.             char    *result=NULL;
  147.             long    errlevel=0;
  148.  
  149.                 nextchar=stptok(ARG0(rmsg),cBuf,24," ,");
  150.                 if (*nextchar) nextchar++;
  151.  
  152.                 if (!stricmp("CLICK",cBuf)) {
  153.                     nextchar=stptok(nextchar,cBuf,24," ,");
  154.                     if (*nextchar) nextchar++;
  155.  
  156.                     if (!stricmp("LEFT",cBuf)) {
  157.                         ClickButton(&rxpc, IECODE_LBUTTON);
  158.                     }
  159.                     else if (!stricmp("MIDDLE",cBuf)) {
  160.                         ClickButton(&rxpc, IECODE_MBUTTON);
  161.                     }
  162.                     else if (!stricmp("RIGHT",cBuf)) {
  163.                         ClickButton(&rxpc, IECODE_RBUTTON);
  164.                     }
  165.                     else {
  166.                         /* Default to left button */
  167.                         ClickButton(&rxpc, IECODE_LBUTTON);
  168.                     }
  169.                 }
  170.                 else if (!stricmp("MOVE",cBuf)) {
  171.                     nextchar=stptok(nextchar,cBuf,24," ,");
  172.                     if (*nextchar) nextchar++;
  173.  
  174.                     length=stcd_i(cBuf,&x);
  175.  
  176.                     nextchar=stptok(nextchar,cBuf,24," ,");
  177.                     if (*nextchar) nextchar++;
  178.  
  179.                     length=stcd_i(cBuf,&y);
  180.  
  181.                     MovePointer(&rxpc, (LONG) x,(LONG) y);
  182.                 }
  183.                 else if (!stricmp("VERSION",cBuf)) {
  184.                     result="mouse v1.0";
  185.                 }
  186.                 else if (!stricmp("QUIT",cBuf)) {
  187.                     loopflag=FALSE;
  188.                 }
  189.                 else {
  190.                     error="Unknown command";
  191.                     errlevel=20;
  192.                 }
  193.  
  194.                 if (error) {
  195.                     SetARexxLastError(rxpc.RexxStuff,rmsg,error);
  196.                 }
  197.                 ReplyARexxMsg(rxpc.RexxStuff,rmsg,result,errlevel);
  198.             }
  199.  
  200.         }
  201.         else loopflag=FALSE;
  202.     }
  203.     Quit(&rxpc, "QUIT command received.\n",0);
  204. }
  205.